<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<header th:replace="admin/header::header-fragment">
</header>
<body>
<shiro:user>
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <div class="layui-form-item">
        <div class="layui-inline">
          角色筛选
        </div>
        <div class="layui-inline">
          <select name="rolename" lay-filter="LAY-user-adminrole-type">
            <option value="-1">全部角色</option>
            <option value="0">管理员</option>
            <option value="1">超级管理员</option>
            <option value="2">纠错员</option>
            <option value="3">采购员</option>
            <option value="4">推销员</option>
            <option value="5">运营人员</option>
            <option value="6">编辑</option>
          </select>
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <div style="padding-bottom: 10px;">
        <button class="layui-btn layuiadmin-btn-role" data-type="batchdel">删除</button>
        <button class="layui-btn layuiadmin-btn-role" data-type="add">添加权限</button>
        <button class="layui-btn layuiadmin-btn-role" data-type="adduser">添加角色权限</button>
      </div>

      <table id="LAY-app-content-comm" lay-filter="LAY-app-content-comm" class="demo"></table>
      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
      </script>
    </div>
  </div>
</div>
</shiro:user>
<shiro:guest>
  <footer th:replace="admin/nologin::header-fragment">
  </footer>
</shiro:guest>
<script th:src="@{/layuiadmin/layui/layui.js}"></script>
<script>
  //JavaScript代码区域
  layui.use('element', function(){
    var element = layui.element;

  });
</script>
<script>
  layui.config({
    base: 'layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'useradmin', 'table'], function(){
    var $ = layui.$
            ,form = layui.form
            ,table = layui.table;

    //第一个实例
    table.render({
      elem: '.demo'
      ,url: 'admin/allRoleList',
      page: true //开启分页
      ,cols: [
        [
                {type: 'checkbox',width:40},
        {field:'blogUsername', title:'用户名', width:100},
        {field:'roleName', title:'权限', width:100},
          {field:'right', title:'操作',toolbar:'#table-useradmin-admin',unresize:true, align:'center',width: 160}
        ]
      ]
    });

    //事件
    var active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-app-content-comm')
                ,checkData = checkStatus.data; //得到选中的数据

        if(checkData.length === 0){
          return layer.msg('请选择数据');
        }

        layer.confirm('确定删除吗？', function(index) {
          $.ajax({
            url:'admin/deleteComment',
            data:{"delete":JSON.stringify(checkData)},
            dataType:'json',
            method:'post',
            success:function (res) {
              if (res.success){
                layer.msg('删除成功', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }else {
                layer.msg('删除失败', {
                  offset: '15px'
                  ,icon: 1
                  ,time: 1000
                });
              }
              table.reload('LAY-app-content-comm');
            }
            ,error: function(){
              layer.alert('系统错误，请联系管理员！', {
                icon: 2,
                skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
              });
            }
          });
          table.reload('LAY-app-content-comm');
        });
      },
      add: function(){
        layer.open({
          type: 2
          ,title: '添加权限'
          ,content: 'role/findroleJueSe'
          ,area: ['500px', '480px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
            //获取checkbox数据,组装成数组!!!!!!
           /* var limits =[];
            $('input[name="limits"]:checked').each(function(index, domEle){
              limits.push($(this).val());
            });
            data.field.limits = limits;*/
            var body = layer.getChildFrame('body', index);
            var rolep = body.find('select[name="rolename"]').val()
            var limits =[];
            var roles = body.find('input[class="check2"]:checked').each(function(index, domEle){
              limits.push($(this).val());
            });
            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              //获取checkbox[name='foodId']的值，获取所有选中的复选框，并将其值放入数组中
              var field = data.field; //获取提交的字段
              var json = JSON.stringify(limits);
              $.post('role/AllRole',{
                "rolep":rolep,"limits":json
              },function (res){
                if (res.success){
                  layer.msg('添加成功', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  });
                }else {
                  layer.msg('添加失败', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  });
                }
              },'json');

              table.reload('LAY-user-back-role');
              layer.close(index); //关闭弹层
            });

            submit.trigger('click');
          }
        });
      },
      adduser: function(){
        layer.open({
          type: 2
          ,title: '添加新角色'
          ,content: 'role/findroleUser'
          ,area: ['400px', '300px']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
            var body = layer.getChildFrame('body', index);
            var roleUsername = body.find('select[name="roleUsername"]').val()
            var rolejuese = body.find('select[name="rolejuese"]').val()
            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
              //获取checkbox[name='foodId']的值，获取所有选中的复选框，并将其值放入数组中
              var field = data.field; //获取提交的字段
              $.post('role/addRoleUser',{
                "roleUsername":roleUsername,"rolejuese":rolejuese
              },function (res){
                if (res.success){
                  layer.msg('添加成功', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  });
                }else {
                  layer.msg('添加失败', {
                    offset: '15px'
                    ,icon: 1
                    ,time: 1000
                  });
                }
              },'json');

              table.reload('LAY-user-back-role');
              layer.close(index); //关闭弹层
            });

            submit.trigger('click');
          }
        });
      }
    }
    $('.layui-btn.layuiadmin-btn-role').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
</body>
</html>

